package com.jinsi.service.ylt.iml;


import com.jinsi.mapper.ylt.UserCouponMapper;
import com.jinsi.pojo.SyDiscountsIssuance;
import com.jinsi.service.ylt.UserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserCouponServiceImpl
 * @Content 用户优惠券业务层
 * @Author 姚龙腾
 * @Date 2022/10/12 11:53
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserCouponServiceImpl implements UserCouponService {

    @Autowired
    private UserCouponMapper userCouponMapper;

    /**
     * 获取所有用户优惠劵信息
     * @param userAccount
     * @param userName
     * @return List<SyDiscountsIssuance>
     */
    @Override
    public List<SyDiscountsIssuance> getAll(String userAccount, String userName) {
        return userCouponMapper.getAll(userAccount, userName);
    }


    /**
     * 获取优惠劵剩余数量
     * @param couponArray
     * @param userArrayLength
     * @return List<Integer>
     */
    @Override
    public List<Integer> getCouponNum(String[] couponArray, Integer userArrayLength) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < couponArray.length; i++){
            Integer id = Integer.valueOf(couponArray[i]);
            int couponNum = userCouponMapper.getCouponNum(id);
            if (couponNum < userArrayLength){
                list.add(id);
            }
        }
        return list;
    }


}
